package com.webkaifa.test;

import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;

import com.webkaifa.socket.BasePacket;




public class ImPacketHeader extends BasePacket{
	public int m_nLength;
	public int m_nMsgType;
	public int m_nSeq;
	public int m_nStatus;
	
	public final static int headLength(){
		return 16;
	}
	public  final static int packetLength(ByteBuffer buffer)
	{
		return buffer.getInt();
	}
	public int packetLength()
	{
		return m_nLength;
	}
	public final static BasePacket creat(ByteBuffer buffer)
	{
		buffer.mark();
		ImPacketHeader l_oHeader=new ImPacketHeader();
		l_oHeader.packetRead(buffer);
		buffer.reset();
		ImPacketHeader l_oPacket=null;
		switch(l_oHeader.m_nMsgType)
		{
			case ImNetMsgCode.IM_NOTIFY_REQ:
				l_oPacket=new ImNotifyReqPacket();
				break;
			case ImNetMsgCode.IM_NOTIFY_RESP:
				l_oPacket=new ImNotifyResPacket();
				break;
			default:
				return null;
		}
		if(l_oPacket.packetRead(buffer)!=0)
		{
			return null;
		}
		return l_oPacket;
	}
	public  int packetRead(ByteBuffer buffer)
	{
		try
		{
			m_nLength=buffer.getInt();
			m_nMsgType=buffer.getInt();
			m_nSeq=buffer.getInt();
			m_nStatus=buffer.getInt();
		}
		catch(BufferUnderflowException  e)
		{
			return -1;
		}
		return 0;
	}
	public  int packetWrite(ByteBuffer buffer)
	{
		try
		{
			buffer.putInt(m_nLength);
			buffer.putInt(m_nMsgType);
			buffer.putInt(m_nSeq);
			buffer.putInt(m_nStatus);
		}
		catch(BufferUnderflowException  e)
		{
			return -1;
		}
		return 0;
	}
}
